#region File Description
//  XAEnima 
//  License: GNU/GPL
//  Developer: Federico Leonardo Motta
//  File description: Stimolation Options.
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Storage;
using XAEnima.Core.GameManager;
using XAEnima.gui;
using SharedContent;
#endregion


namespace XAEnima.Core.Screens.Menu.MenuOptions
{
    public delegate void VarieAction();
    /// <summary>
    /// Questa componente implementa la voce delle opzioni generiche.
    /// </summary>
    public class Varie : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public event VarieAction OnBack;

        SpriteBatch spriteBatch;
        Texture2D tex;
        Vector2 pos_tex = Vector2.Zero;
        UIwin win;
        UIbutton Indietro, Save;
        List<UIlabel> Stimolatori = new List<UIlabel>();
        Boolean stim;
        IInputManager InputManagerService;
        float screenscale;
        int res, size;
        Rectangle area;
        SharedOptions optbuf = new SharedOptions();
        
        StorageDevice device;
        int index = 0;

        public Varie(Game game)
            : base(game)
        {
            InputManagerService = (IInputManager)Game.Services.GetService(typeof(IInputManager));
            InputManagerService.EnterPressed += new PressedEventHandler(EnterIsPressed);
            InputManagerService.EnterReleased += new PressedEventHandler(EnterIsReleased);
            InputManagerService.TabPressed += new PressedEventHandler(TabIsPressed);
            InputManagerService.UpPressed += new PressedEventHandler(UpIsPressed);
            InputManagerService.DownPressed += new PressedEventHandler(DownIsPressed);
            InputManagerService.LeftPressed += new PressedEventHandler(LeftIsPressed);
            InputManagerService.RightPressed += new PressedEventHandler(RightIsPressed);
            
            
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            FileManager fm = new FileManager();

            optbuf = fm.GetOptions(device);
            screenscale = (float)(Game.GraphicsDevice.Viewport.Width / 1600f);
            spriteBatch = (Game.Services.GetService(typeof(SharedContext)) as SharedContext).sprite_batch;
            area = Game.GraphicsDevice.Viewport.TitleSafeArea;

            tex = Game.Content.Load<Texture2D>(@"background1600");
            win = new UIwin(Game, "MainWin", new Vector2(0, 0), tex);
            float x_pos = (float)Game.GraphicsDevice.Viewport.Width / 2.82f;
            float y_pos = (float)Game.GraphicsDevice.Viewport.Height / 3;

            Indietro = new UIbutton("Indietro", new Vector2(x_pos * 0.9f, y_pos * (1 + 1.1f)), "Indietro", "Arial30",
                                           "GUI/button/button", "GUI/button/button_pres",
                                           "GUI/button/button_sel", "GUI/button/button_inact",
                     Color.Black, Color.DarkGray,
                                           Game);

            Save = new UIbutton("Salva", new Vector2(x_pos * 1.5f, y_pos * (1 + 1.1f)), "Salva", "Arial30",
                    "GUI/button/button", "GUI/button/button_pres",
                    "GUI/button/button_sel", "GUI/button/button_inact",
                     Color.Black, Color.DarkGray,
                    Game);

             UIlabel lab = new UIlabel("Stimolatori", "Stimolatori:", "Arial", Game);
             Stimolatori.Add(lab);
             lab = new UIlabel("Esterni", "Esterni", "Arial", Game);
             lab.Select = (optbuf.Stimolation) ? true : false;
             Stimolatori.Add(lab);
             lab = new UIlabel("Internii", "Interni", "Arial", Game);
             lab.Select = (optbuf.Stimolation) ? false : true;
             Stimolatori.Add(lab);
             stim = optbuf.Stimolation;
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            Indietro.UpdateButton(gameTime);
            Save.UpdateButton(gameTime);
            base.Update(gameTime);
        }

        protected override void Dispose(bool disposing)
        {
            Game.Content.Unload();
            Game.Services.RemoveService(typeof(Options));
            base.Dispose(disposing);
        }

        public void Detach()
        {
            // Detach the event and delete the list
            InputManagerService.EnterPressed -= new PressedEventHandler(EnterIsPressed);
            InputManagerService.EnterReleased -= new PressedEventHandler(EnterIsReleased);
            InputManagerService.UpPressed -= new PressedEventHandler(UpIsPressed);
            InputManagerService.DownPressed -= new PressedEventHandler(DownIsPressed);
            InputManagerService.LeftPressed -= new PressedEventHandler(LeftIsPressed);
            InputManagerService.RightPressed -= new PressedEventHandler(RightIsPressed);
            InputManagerService = null;
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(win.Skin, win.Position, null, Color.White, 0f, Vector2.Zero, screenscale, SpriteEffects.None, 0f);
            Int16 x = 0;
            foreach (UIlabel label in Stimolatori)
            {
                Color col = (label.Select) ? Color.SlateGray : Color.Black;
                spriteBatch.DrawString(label.Font, label.Text, new Vector2(Indietro.Position.X * 0.55f + (x * 320) * screenscale, Indietro.Position.Y - 200 * screenscale), col, 0.0f, new Vector2(0, 0), ((index == 0) ? screenscale * 1.6f : screenscale * 1.2f), SpriteEffects.None, 0);
                res = (label.Select) ? x : res;
                x++;
            }
            // Disegno i bottoni del Menu Opzioni attraverso il loro metodo Draw
            Indietro.Draw(gameTime, spriteBatch, screenscale * 1.35f);
            Save.Draw(gameTime, spriteBatch, screenscale * 1.35f);
            spriteBatch.End();

            base.Draw(gameTime);
        }

        private void EnterIsPressed()
        {

            if (Indietro.Select)
            {
                Indietro.IsPressed = true;

            }
            else if (Save.Select)
            {
                Save.IsPressed = true;
            }
        }

        private void EnterIsReleased()
        {
            if (Indietro.Select)
            {
                Indietro.IsPressed = false;
                if (OnBack != null) { OnBack(); }
            }
            else if (Save.Select)
            {
                Save.IsPressed = false;
                FileManager fm = new FileManager();
                SharedOptions opt = new SharedOptions();
                opt.IsFullscreen = optbuf.IsFullscreen;
                opt.ResolutionX = optbuf.ResolutionX;
                opt.ResolutionY = optbuf.ResolutionY;
                opt.Stimolation = stim;

                fm.DoSave(device, opt);


            }
        }

        private void TabIsPressed() { }

        private void UpIsPressed()
        {
            if (Indietro.Select)
            {
                Indietro.Select = false;
                index = 0;
            }
        }

        private void DownIsPressed()
        {
            if (index == 0)
            {
                index = 1;
                Indietro.Select = true;
            }
        }

        private void LeftIsPressed()
        {
            if (Indietro.Select)
            {
                Indietro.Select = false;
                Save.Select = true;
            }
            else if (Save.Select)
            {
                Save.Select = false;
                Indietro.Select = true;
            }
            else if (index == 0)
            {
                if (Stimolatori.ElementAt(1).Select)
                {
                    Stimolatori.ElementAt(1).Select = false;
                    Stimolatori.ElementAt(2).Select = true;
                    stim = false;
                }
                else if (Stimolatori.ElementAt(2).Select)
                {
                    Stimolatori.ElementAt(2).Select = false;
                    Stimolatori.ElementAt(1).Select = true;
                    stim = true;
                }
            }
        }

        private void RightIsPressed()
        {
            if (Indietro.Select)
            {
                Indietro.Select = false;
                Save.Select = true;
            }
            else if (Save.Select)
            {
                Save.Select = false;
                Indietro.Select = true;
            }
            else if (index == 0)
            {
                if (Stimolatori.ElementAt(1).Select)
                {
                    Stimolatori.ElementAt(1).Select = false;
                    Stimolatori.ElementAt(2).Select = true;
                    stim = false;
                }
                else if (Stimolatori.ElementAt(2).Select)
                {
                    Stimolatori.ElementAt(2).Select = false;
                    Stimolatori.ElementAt(1).Select = true;
                    stim = true;
                }
            }
        }
    }
}